package com.ruitu.health_1.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.support.widget.progress.ProgressLinearLayout;
import com.ruitu.health_1.R;
import com.ruitu.health_1.dao.bean.Category;
import com.ruitu.health_1.mvp.model.CategoryModel;
import com.ruitu.health_1.mvp.presenter.CategoryPresenter;
import com.ruitu.health_1.ui.ArticleListActivity;

import java.util.ArrayList;
import java.util.List;

import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.adapter.TabAdapter;
import q.rorbin.verticaltablayout.widget.ITabView;
import q.rorbin.verticaltablayout.widget.QTabView;
import q.rorbin.verticaltablayout.widget.TabView;


public class CategoryFragment extends BaseFragment<CategoryPresenter.CategoryListP
        , CategoryModel.CategoryListM> implements VerticalTabLayout.OnTabSelectedListener {

    private VerticalTabLayout v_tab;
    private ViewPager2 v_view_pager;
    private List<Category> datas = new ArrayList<>();
    private LinearLayout ll_search;
    private ProgressLinearLayout progress;
    private LRecyclerView l_recyclerview;

    public static CategoryFragment newInstance() {
        Bundle args = new Bundle();
        CategoryFragment fragment = new CategoryFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_category, container, false);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        p.getCategory();
        v_tab = view.findViewById(R.id.v_tab);
        v_view_pager = view.findViewById(R.id.v_view_pager);
        ll_search = view.findViewById(R.id.ll_search);
        l_recyclerview = view.findViewById(R.id.l_recyclerview);
//        view_page.setScanScroll(false);
        setOnClickListener(ll_search);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (ll_search == view) {
            startActivity(ArticleListActivity.class);
        }
    }

    public void setData(List<Category> datas) {
        this.datas = datas;
        initData();
        setListener();
    }

    private void setListener() {

        v_tab.setTabAdapter(new TabAdapter() {
            @Override
            public int getCount() {
                return datas.size();
            }

            @Override
            public ITabView.TabBadge getBadge(int position) {

                return null;
            }

            @Override
            public ITabView.TabIcon getIcon(int position) {
                return null;
            }

            @Override
            public ITabView.TabTitle getTitle(int position) {
                QTabView.TabTitle tabTitle = new QTabView.TabTitle.Builder()
                        .setContent(datas.get(position).getName())
                        .setTextColor(getResources().getColor(R.color.black), getResources().getColor(R.color.default_text_color))
                        .setTextSize(15)
                        .build();

                return tabTitle;
            }

            @Override
            public int getBackground(int position) {
                return R.drawable.selector_category_btn;
            }
        });
        v_tab.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {
                v_view_pager.setCurrentItem(position);
            }

            @Override
            public void onTabReselected(TabView tab, int position) {

            }
        });
        v_view_pager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                v_tab.setTabSelected(position);
            }
        });

    }

    public void initData() {

        FragmentStateAdapter adapter = new FragmentStateAdapter(this) {
            @Override
            public int getItemCount() {
                return datas.size();
            }

            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return new Category2Fragment().newInstance(datas.get(position).getId());
            }

        };

        v_view_pager.setAdapter(adapter);
    }

    @Override
    public void onTabSelected(TabView tab, int position) {

    }

    @Override
    public void onTabReselected(TabView tab, int position) {

    }


}
